<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Merchant;
use App\Models\User;
use Dingo\Api\Http\Request;

class MerchantController extends BaseController
{

    public $model = Merchant::class;

    public function index(Request $request) {
        $contact = $request->input('contact');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');

        $users = Merchant::when($contact, function ($q)use($contact) {
            $q->where('contact', 'like', '%'. $contact . '%');
        })->startAt($start_at)->endAt($end_at)->orderBy('audit_status', 'asc')->orderBy('id', 'desc')
            ->paginate(per_page());
        return $this->paginate($users);
    }

    public function _after_editStatus($item, $action, $value, $reason) {
        if ($action != 'audit')
            return;
        $user_id = $item->user_id;
        if ($value == 1) {
            $user = User::find($user_id);
            $user->is_merchant = 1;
            $user->save();
        }
    }

}

